package com.tommy.jiami.adapter;

import android.content.*;
import android.view.*;
import android.widget.*;
import com.tommy.jiami.*;
import java.io.*;
import java.util.*;
import static com.tommy.jiami.utils.Constants.*;
import static com.tommy.jiami.utils.Utils.*;

public class FileAdapter extends BaseAdapter
{
	Context context;
	List<File> fileList;

	public FileAdapter(Context context, List<File> files)
	{
		this.context = context;
		fileList = files;
	}
	
	public FileAdapter(Context context,  File[] files)
	{
		this(context,Arrays.asList(files));
	}
	
	@Override
	public int getCount()
	{
		return fileList.size();
	}
	
	@Override
	public long getItemId(int position)
	{
		return position;
	}
	
	@Override
	public Object getItem(int position)
	{
		return fileList.get(position);
	}
	
	@Override
	public View getView(int position, View v, ViewGroup parent)
	{
		v=LayoutInflater.from(context).inflate(R.layout.item_file,null);
		File f=fileList.get(position);
		TextView name=(TextView)v.findViewById(R.id.name);
		TextView length=(TextView)v.findViewById(R.id.length);
		ImageView image=(ImageView)v.findViewById(R.id.image);
		String strname=f.getName();
		int i=0;
		if(f.isFile()){
			i=R.drawable.txt;
		}else if(f.isDirectory()){
			i=R.drawable.dic;
		}
		image.setImageResource(i);
		name.setText(strname);
		long len=f.length();
		String lengthStr=
		len>GBl?round(len*1.0/GBl,2)+GBs:
		len>MBl?round(len*1.0/MBl,2)+MBs:
		len>KBl?round(len*1.0/KBl,2)+KBs:
		len+Bs;
		if(f.isFile())length.setText(lengthStr);
		return v;
	}
	
	public void notifyDataSetChanged(List<File> files)
	{
		fileList=files;
		notifyDataSetChanged();
	}
	
	public void notifyDataSetChanged(File[] files)
	{
		notifyDataSetChanged(Arrays.asList(files));
	}
	
	public List<File> getFileList()
	{
		return fileList;
	}
}
